
clear all

local path0="C:\Dropbox\GeneticsProject (1)\REStat\round_accepted_replication_files\"
local path1="C:\Dropbox\GeneticsProject (1)\REStat\round_accepted_replication_files\outregs"

capture cd "`path0'Workfiles"
set more off
use completed_data, clear

	* create settler mortality with the African data corrected based on Albouy
	capture drop logmort3
	gen logmort3=logmort2
	replace logmort3=logmort if countrycode=="SGP" | countrycode=="AUS"
	
	gen lnUKdist=log(geo_UK_distw)

	
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Table 2: Robustness checks - alternative IV and sources of information
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	ivreg2 pwt_ln_rgdpwok (idv = distM_USA) if continent~=., robust ffirst
	matrix A=e(first)
	local R2first=A[2,1]
	local Ffirst =A[3,1]
	outreg2 idv using "`path1'\RR_appendix_table_D6.txt" , ///
		replace bdec(3) rdec(3) aster nocons nolabel ctitle("IV: Mahalanobis US") ///
		addstat("1st stage F-stat", `Ffirst',"Partial R2",`R2first')

	ivreg2 pwt_ln_rgdpwok (idv = bloodA bloodB) if continent~=., robust ffirst	
	matrix A=e(first)
	local R2first=A[2,1]
	local Ffirst =A[3,1]
	outreg2 idv using "`path1'\RR_appendix_table_D6.txt" , ///
		append bdec(3) rdec(3) aster nocons nolabel ctitle("IV: separate") ///
		addstat("1st stage F-stat", `Ffirst',"Partial R2",`R2first',"Over-id p-val",`e(jp)')

	ivreg2 pwt_ln_rgdpwok (idv = distM_GBR_wiki)  if continent~=., robust ffirst
	matrix A=e(first)
	local R2first=A[2,1]
	local Ffirst =A[3,1]
	outreg2 idv using "`path1'\RR_appendix_table_D6.txt" , ///
		append bdec(3) rdec(3) aster nocons nolabel ctitle("IV: wiki") ///
		addstat("1st stage F-stat", `Ffirst',"Partial R2",`R2first')
		
		
	ivreg2 pwt_ln_rgdpwok (idv =  SP_UK_fst_distance_dominant)  if continent~=., robust ffirst
	matrix A=e(first)
	local R2first=A[2,1]
	local Ffirst =A[3,1]
	outreg2 idv using "`path1'\RR_appendix_table_D6.txt" , ///
		append bdec(3) rdec(3) aster nocons nolabel ctitle("IV: SP first") ///
		addstat("1st stage F-stat", `Ffirst',"Partial R2",`R2first')
		
		
	ivreg2 pwt_ln_rgdpwok (idv =   SP_UK_nei_distance_dominant)  if continent~=., robust ffirst
	matrix A=e(first)
	local R2first=A[2,1]
	local Ffirst =A[3,1]
	outreg2 idv using "`path1'\RR_appendix_table_D6.txt" , ///
		append bdec(3) rdec(3) aster nocons nolabel ctitle("IV: SP nei") ///
		addstat("1st stage F-stat", `Ffirst',"Partial R2",`R2first')
		
 
	ivreg2 pwt_ln_rgdpwok (idv = lgh_pronoun)  if continent~=., robust ffirst
	matrix A=e(first)
	local R2first=A[2,1]
	local Ffirst =A[3,1]
	outreg2 idv using "`path1'\RR_appendix_table_D6.txt" , ///
		append bdec(3) rdec(3) aster nocons nolabel ctitle("IV: pronoun") ///
		addstat("1st stage F-stat", `Ffirst',"Partial R2",`R2first')
		
 	ivreg2 pwt_ln_rgdpwok (idv = lgh_pronoun distM_UK)  if continent~=., robust ffirst
	matrix A=e(first)
	local R2first=A[2,1]
	local Ffirst =A[3,1]
	outreg2 idv using "`path1'\RR_appendix_table_D6.txt" , ///
		append bdec(3) rdec(3) aster nocons nolabel ctitle("IV: pronoun + blood") ///
		addstat("1st stage F-stat", `Ffirst',"Partial R2",`R2first',"Over-id p-val",`e(jp)')
		

*=========================================================================
*					Pronoun drop: boostrap J-stat
*=========================================================================

use completed_data, clear
set seed 1234567 

drop if countrycode=="SGP"
drop if countrycode=="HKG"

* exclude small countries (here Singapore)
ivreg2  pwt_ln_rgdpwok (idv = bloodA bloodB), first gmm2s robust
local NN=e(N)
keep if e(sample)
gen z1=bloodA
gen z2=bloodB
local J_bloodAB=e(j)
di `J_bloodAB'

keep pwt_ln_rgdpwok idv z1 z2
save temp_book0, replace

ivreg2  pwt_ln_rgdpwok (idv= z1 z2), first gmm2s robust

matrix eW=e(W)
predict res0, resid

gen m0=z1*res0
sum m0
local m0s=r(mean)

gen m1=z2*res0
sum m1
local m1s=r(mean)

gen m2=res0
sum m2
local m2s=r(mean)

matrix A0=(`m0s' \ `m1s' \ `m2s')

matrix JJ=A0'*eW*A0*`NN'
local J0=JJ[1,1]
di `J0'

**** start the bootstrap cycle

tempname sim
postfile `sim' JX using boot_results_bloodAB, replace
quietly {
	forvalues i = 1/1000 {
		use temp_book0, clear
		bsample
		ivreg2  pwt_ln_rgdpwok (idv= z1 z2), first gmm2s robust
		
		matrix eW=e(W)
		predict res0, resid

		gen m0=z1*res0
		sum m0
		local m0s=r(mean)

		gen m1=z2*res0
		sum m1
		local m1s=r(mean)

		gen m2=res0
		sum m2
		local m2s=r(mean)

		matrix A1=(`m0s' \ `m1s' \ `m2s')


		matrix JJ=(A1-A0)'*eW*(A1-A0)*`NN'
		local JX=JJ[1,1]		
		if mod(`i',100)==0 {
			noisily di "`i'"
			}
			
		post `sim' (`JX')
	}
}
postclose `sim'


		
		
*=========================================================================
*					Pronoun drop: boostrap J-stat
*=========================================================================

use completed_data, clear
set seed 1234567 

drop if countrycode=="SGP"
drop if countrycode=="HKG"

* exclude small countries (here Singapore)
ivreg2  pwt_ln_rgdpwok (idv=  lgh_pronoun distM_UK), first gmm2s robust
local NN=e(N)
keep if e(sample)
gen z1=lgh_pronoun
gen z2=distM_UK
local J_pronoun=e(j)
di `J_pronoun'

keep pwt_ln_rgdpwok idv z1 z2
save temp_book0, replace

ivreg2  pwt_ln_rgdpwok (idv= z1 z2), first gmm2s robust

matrix eW=e(W)
predict res0, resid

gen m0=z1*res0
sum m0
local m0s=r(mean)

gen m1=z2*res0
sum m1
local m1s=r(mean)

gen m2=res0
sum m2
local m2s=r(mean)

matrix A0=(`m0s' \ `m1s' \ `m2s')

matrix JJ=A0'*eW*A0*`NN'
local J0=JJ[1,1]
di `J0'

**** start the bootstrap cycle

tempname sim
postfile `sim' JX using boot_results_pronoun, replace
quietly {
	forvalues i = 1/1000 {
		use temp_book0, clear
		bsample
		ivreg2  pwt_ln_rgdpwok (idv= z1 z2), first gmm2s robust
		
		matrix eW=e(W)
		predict res0, resid

		gen m0=z1*res0
		sum m0
		local m0s=r(mean)

		gen m1=z2*res0
		sum m1
		local m1s=r(mean)

		gen m2=res0
		sum m2
		local m2s=r(mean)

		matrix A1=(`m0s' \ `m1s' \ `m2s')


		matrix JJ=(A1-A0)'*eW*(A1-A0)*`NN'
		local JX=JJ[1,1]		
		if mod(`i',100)==0 {
			noisily di "`i'"
			}
			
		post `sim' (`JX')
	}
}
postclose `sim'



**=================================================================
*					Evaluate p-values
**=================================================================


noisily di "pronoun drop"
use boot_results_pronoun, clear
gen t0=(JX>`J_pronoun')
sum t0


noisily di "blood A& B separately"
use boot_results_bloodAB, clear
gen t0=(JX>`J_bloodAB')
sum t0

